package test;





import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.*;//导包

 

 

/**

 * <p>Title: 段静迪的猜字游戏程序</p>

 *

 * <p>Description: </p>

 *

 * <p>Copyright: Copyright (c) 2007</p>

 *

 * <p>Company: </p>

 *

 * @author 段静迪

 * @version 1.0

 */

public class GameWindow

    extends JFrame {

  JPanel contentPane;

  JTextField jTextField1 = new JTextField();

  JLabel jLabel1 = new JLabel();

  JButton jButton1 = new JButton();

  JButton jButton2 = new JButton();

  JLabel jLabel2 = new JLabel();

  JLabel jLabel3 = new JLabel();

  Random random =new Random();//生成随机数生成器

  private int Rvalue =0 ;

  private int timer =0;//计数器清零

  public GameWindow() {

    try {

      setDefaultCloseOperation(EXIT_ON_CLOSE);

      jbInit();

    }

    catch (Exception exception) {

      exception.printStackTrace();

    }

  }

 

  /**

   * Component initialization.

   *

   * @throws java.lang.Exception

   */

  private void jbInit() throws Exception {

    contentPane = (JPanel) getContentPane();

    contentPane.setLayout(null);

    setSize(new Dimension(515, 365));

    setTitle("猜字游戏程序");

    jTextField1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));

    jTextField1.setText("");

    jTextField1.setBounds(new Rectangle(235, 77, 147, 43));

    jLabel1.setText("猜测结果提示：");

    jLabel1.setBounds(new Rectangle(23, 135, 127, 42));

    jButton1.setBounds(new Rectangle(113, 219, 128, 50));

    jButton1.setText("生成随机数");

    jButton1.addActionListener(new GameWindow_jButton1_actionAdapter(this));

    jButton2.setBounds(new Rectangle(265, 219, 83, 50));

    jButton2.setText("猜数");

    jButton2.addActionListener(new GameWindow_jButton2_actionAdapter(this));

    jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));

    jLabel2.setText("输入一个10~20之间的数");

    jLabel2.setBounds(new Rectangle(26, 72, 207, 59));

    jLabel3.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));

    jLabel3.setText("还没有生成随机数");

    jLabel3.setBounds(new Rectangle(181, 138, 150, 42));

    contentPane.add(jButton1);

    contentPane.add(jButton2);

    contentPane.add(jLabel3);

    contentPane.add(jLabel2);

    contentPane.add(jLabel1);

    contentPane.add(jTextField1);

  }

 

  public void jButton1_actionPerformed(ActionEvent e) {

    Rvalue = 10+ random.nextInt(11);//生成一个10~20之间的随机数，并且保存在Rvalue中

    jLabel3.setText("随机数已生成");//提示用户

   

 

  }

 

  public void jButton2_actionPerformed(ActionEvent e) {

    //判断猜测结果

    if(Integer.valueOf(jTextField1.getText())==Rvalue )

    {

      jLabel3.setText("猜中了");

      return;

    }

    else if( Integer.valueOf(jTextField1.getText())<=Rvalue )

    {

      jLabel3.setText("猜小了");

      return;

     

    }

    else

    {

      jLabel3.setText("猜大了");

      return;

    }   

  }

}

 

class GameWindow_jButton2_actionAdapter

    implements ActionListener {

  private GameWindow adaptee;

  GameWindow_jButton2_actionAdapter(GameWindow adaptee) {

    this.adaptee = adaptee;

  }

 

  public void actionPerformed(ActionEvent e) {

    adaptee.jButton2_actionPerformed(e);

  }

}

 

class GameWindow_jButton1_actionAdapter

    implements ActionListener {

  private GameWindow adaptee;

  GameWindow_jButton1_actionAdapter(GameWindow adaptee) {

    this.adaptee = adaptee;

  }

 

  public void actionPerformed(ActionEvent e) {

 

    adaptee.jButton1_actionPerformed(e);

  }

}
